package com.sjq.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjq.mapper.PaperMapper;
import com.sjq.mapper.RatingMapper;
import com.sjq.mapper.RoleMapper;
import com.sjq.model.entity.Paper;
import com.sjq.model.entity.PaperBase;
import com.sjq.model.entity.Rating;
import com.sjq.model.request.ContentDto;
import com.sjq.model.request.PaperRequest;
import com.sjq.model.request.UserPaperRequest;
import com.sjq.model.response.PaperByManagerResponse;
import com.sjq.model.response.PaperDetailResponse;
import com.sjq.service.IPaperService;
import com.sjq.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 稿件Service业务层处理
 *
 * @author sjq
 * @date 2022-03-13
 */
@Service
public class PaperServiceImpl implements IPaperService
{
    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RatingMapper ratingMapper;

    /**
     * 查询稿件
     *
     * @param id 稿件ID
     * @return 稿件
     */
    @Override
    public PaperDetailResponse selectPaperById(Long id)
    {
        PaperDetailResponse paperDetailResponse = new PaperDetailResponse()
                .setPaper(paperMapper.selectPaperById(id))
                .setRatingList(ratingMapper.selectRatingListByPaperId(id));
        return paperDetailResponse;
    }

    /**
     * 查询稿件列表
     *
     * @param paperRequest 稿件
     * @return 稿件
     */
    @Override
    public PaperByManagerResponse selectPaperList(PaperRequest paperRequest)
    {
        PageHelper.startPage(paperRequest.getPageNum(),paperRequest.getPageSize());
        List<Paper> list = paperMapper.selectPaperList(paperRequest);
        PageInfo<Paper> selectPage = new PageInfo<Paper>(list);
        PaperByManagerResponse paperByManagerResponse = new PaperByManagerResponse()
                .setPaperList(list)
                .setPages(selectPage.getPages())
                .setPageNum(selectPage.getPageNum())
                .setPageSize(selectPage.getPageSize())
                .setTotal(selectPage.getTotal());
        return paperByManagerResponse;
    }



    /**
     * 新增稿件
     *
     * @param paper 稿件
     * @return 结果
     */
    @Override
    public int insertPaper(PaperBase paper)
    {
        Long userId = userUtil.getUserId();
        Paper newPaper = new Paper()
                .setUserId(userId)
//                .setUserRole(roleMapper.gerRoleByUserId(userId))
                .setTime(new Date())
                .setClassificationId(paper.getClassificationId())
                ;
        newPaper.setContent(paper.getContent())
        .setFilePath(paper.getFilePath())
        .setTitle(paper.getTitle());
        return paperMapper.insertPaper(newPaper);
    }

    /**
     * 修改稿件
     *
     * @param paper 稿件
     * @return 结果
     */
    @Override
    public int updatePaper(Paper paper)
    {
        paper.setUserId(userUtil.getUserId());
        return paperMapper.updatePaper(paper);
    }

    /**
     * 批量删除稿件
     *
     * @param ids 需要删除的稿件ID
     * @return 结果
     */
    @Override
    public int deletePaperByIds(Long[] ids)
    {
        return paperMapper.deletePaperByIds(ids);
    }

    /**
     * 删除稿件信息
     *
     * @param id 稿件ID
     * @return 结果
     */
    @Override
    public int deletePaperById(Long id)
    {
        Long userId = userUtil.getUserId();
        return paperMapper.deletePaperById(id,userId);
    }

    /**
     * 批量修改征稿
     * @param ids
     * @return
     */
    @Override
    public int updatePaperStatus(Long[] ids){
        return paperMapper.updatePaperStatus(ids);
    }

    /**
     * 批量修改审稿
     * @param ids
     * @return
     */
    @Override
    public int updatePaperExpertStatus(Long[] ids){
        return paperMapper.updatePaperExpertStatus(ids);
    }


    @Override
    public int insertZhengGao(Long userId, Long[] ids){
        return paperMapper.insertZhengGao(userId,ids);
    }

    /**
     * 查询稿件列表
     */
    @Override
    public List<Paper> paperList(ContentDto contentdto){
        return paperMapper.paperList(contentdto);
    }


    @Override
    public List<Paper> paperListByMyself(ContentDto contentdto, Long userId){
        UserPaperRequest userPaperRequest = new UserPaperRequest()
                .setUserId(userId);
        BeanUtils.copyProperties(contentdto,userPaperRequest);
        return paperMapper.paperListByMyself(userPaperRequest);
    }

    /**
     * 专家评价
     * @param rating
     * @return
     */
    @Override
    public int evaluate(Rating rating){
        rating.setExpertId(userUtil.getUserId());
        return ratingMapper.insertRating(rating);
    }

}
